package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.agent.GetDeveloperDataServiceAgent;
import com.mysoft.gfyx.marketdevelop.mip.agent.dto.InvenstmentRelationCacheDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.*;
import com.mysoft.gfyx.marketdevelop.service.CompanyService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysoft.framework.modeling.dto.Filter;
import com.mysoft.framework.modeling.dto.OptionItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImp extends AppService implements CompanyService {
    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataServiceAgent getDeveloperDataServiceAgent;

    public List<OptionItem> load(List<Filter> filters, Map<String, String> urlParams) {
        List<OptionItem> items = new ArrayList<>();
        if (! urlParams.containsKey(CompanyService.companyCodeQueryName)) {
            return items;
        }

        InvenstmentRelationCacheDto res = getDeveloperDataServiceAgent.getFormativeInvestment(urlParams.get(CompanyService.companyCodeQueryName));

        res.getList().forEach(investment -> {

            OptionItem item = new OptionItem();
            item.setValue(investment.getCreditCode());
            item.setParent(investment.getParentCreditCode());
            item.setText(investment.getCompanyName());

            if (investment.getParentCreditCode() == "") {
                item.setDefault(true);
            }

            items.add(item);
        });

        return items;
    }

    protected OptionItem setDefaultRootDeveloper(String creditCode) {
        OptionItem item = new OptionItem();
        item.setValue(creditCode);
        item.setText("-");
        item.setParent("");
        item.setDefault(true);
        return item;
    }
}
